package main

import "fmt"

type Vec struct {
	x int
	y int
}

func generateMatrix(n int) [][]int {
	//三初始化二维数组
	res := make([][]int, n)
	for i, _ := range res {
		res[i] = make([]int, n)
	}
	fmt.Println(res)
	// 定义向量
	vec := Vec{1, 0}
	x, y := 0, 0
	for i := 0; i < n*n; i++ {
		res[y][x] = i + 1
		for _, v := range res {
			fmt.Println(v)
		}
		fmt.Println(x, y, i, vec)
		if x+vec.x == n ||
			y+vec.y == n ||
			x+vec.x < 0 ||
			y+vec.y < 0 ||
			res[y+vec.y][x+vec.x] != 0 {
			vec = changeVec(vec)
		}

		x = x + vec.x
		y = y + vec.y
		fmt.Println(x, y, i, vec)
	}

	return res
}

func changeVec(v Vec) Vec {
	if v.x == 1 && v.y == 0 {
		return Vec{0, 1}
	} else if v.x == 0 && v.y == 1 {
		return Vec{-1, 0}
	} else if v.x == -1 && v.y == 0 {
		return Vec{0, -1}
	} else if v.x == 0 && v.y == -1 {
		return Vec{1, 0}
	} else {
		return Vec{}
	}
}
func main() {
	generateMatrix(4)
}
